// 配置API接口地址
//var root = 'http://localpshop.com/v1'
//var root = 'http://192.168.1.114/v1'
//var root = 'http://pshop.com/v1'
var root = 'http://pstsc.pstech360.com/'
// 引用axios
var axios = require('axios')
// 自定义判断元素类型JS
function toType (obj) {
  return ({}).toString.call(obj).match(/\s([a-zA-Z]+)/)[1].toLowerCase()
}
// 参数过滤函数
function filterNull (o) {
  for (var key in o) {
    if (o[key] === null) {
      delete o[key]
    }
    if (toType(o[key]) === 'string') {
      o[key] = o[key].trim()
    } else if (toType(o[key]) === 'object') {
      o[key] = filterNull(o[key])
    } else if (toType(o[key]) === 'array') {
      o[key] = filterNull(o[key])
    }
  }
  return o
}
/*
  接口处理函数
*/

function apiAxios (method, url, params, authorization, success, failure) {
  if (params) {
    params = filterNull(params)
  }
  if(authorization === undefined){
  	authorization = ''
  }
  axios({
    method: method,
    url: url,
    data: method === 'POST' || method === 'PUT' ? params : null,
    params: method === 'GET' || method === 'DELETE' ? params : null,
    baseURL: root+"api/",
    withCredentials: true,
    headers: {'Authorization': authorization},
  })
  .then(function (res) {
  	if (success) {
  	  success(res.data)
  	}
    if (res.data.code === 200) {
    } 
  })
}

// 返回在vue模板中的调用接口
export default {
  root: root,
  get: function (url, params, success, failure) {
    return apiAxios('GET', url, params, success, failure)
  },
  post: function (url, params, success, failure) {
    return apiAxios('POST', url, params, success, failure)
  },
  put: function (url, params, success, failure) {
    return apiAxios('PUT', url, params, success, failure)
  },
  delete: function (url, params, success, failure) {
    return apiAxios('DELETE', url, params, success, failure)
  }
}
// 用户信息
export const userInfo = (authorization, success, failure) => {
  return apiAxios('GET', '/client/Members', null, authorization, success, failure)
}